package com.tom.architect02.mnbase.annotation.jvms.juc.juc4;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Added by Tom on 2024/04/22.
 */
public class ClassBlockingQueue<T> {
    // 任务队列
    private Deque<T> queue = new ArrayDeque();

    private ReentrantLock lock = new ReentrantLock();
    //条件变量：队列满(队列满，进行等待)-生产者   集合
    private Condition fullWaitSet = lock.newCondition();
    //条件变量：队列空(队列空，进行等待)-消费者  集合
    private Condition nullWaitSet = lock.newCondition();
    //队列容量，任务数。
    private AtomicInteger capcity;


    public ClassBlockingQueue(AtomicInteger capcity) {
        this.capcity = capcity;
    }

    /**
     * 从队列取任务出去
     * 死等，没有了，就等着。我也不释放
     * 整个程序里面会有2根线程存在
     */
    public T take() {
        lock.lock();
        try {
            while (queue.isEmpty()) {
                nullWaitSet.await();
            }
            T t = queue.removeFirst();
            fullWaitSet.signal();
            return t;
        } catch (InterruptedException e) {
            e.printStackTrace();
            return null;
        } finally {
            lock.unlock();
        }
    }

    /**
     * 超时阻塞获取
     */
    public T poll(long timerout, TimeUnit unit) {
        lock.lock();
        try {
            long nanos = unit.toNanos(timerout);
            while (queue.isEmpty()) {
                if (nanos <= 0) {
                    return null;
                }
                //nanos = 1s  --> 0.5s   ->0.5s
                nanos = nullWaitSet.awaitNanos(nanos);
            }
            T t = queue.removeFirst();
            fullWaitSet.signal();
            return t;
        } catch (InterruptedException e) {
            e.printStackTrace();
            return null;
        } finally {
            lock.unlock();
        }
    }


    /**
     * 提取
     *
     * @param element
     */
    public void put(T element) {
        lock.lock();
        try {
            while (queue.size() >= capcity.intValue()) {
                System.out.println("对象：" + element + "等待中.....");
                fullWaitSet.await();
            }
            queue.addLast(element);
            System.out.println("对象：" + element + "加入队列.....");
            nullWaitSet.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void tryPut(RejectPolicy rejectPolicy, T task) {
        lock.lock();
        try {
            //判断是否队列已满
            if (queue.size() == capcity.intValue()) {
                rejectPolicy.reject(this, task);
                System.out.println("对象：" + task + "执行拒绝策略.....");
            } else {//有空闲
                queue.addLast(task);
                nullWaitSet.signal();
                System.out.println("对象：" + task + "加入队列.....");
            }
        } finally {
            lock.unlock();
        }
    }


}
